import { defineStore } from 'pinia';
import escape2html from '@/api/escape2html';

export interface BaseInfo {
  id: number;
  username: string;
  telephone: string;
  sex: string;
  avator: string;
  token: string;
  mail: string;
}

const { toHtml } = escape2html();

export const userinfo = defineStore({
  id: 'user',
  state: () => ({
    login: false, //登陆状态则为true
    // signup: false, //想要注册则为true
    // findPass: false,
    usercardPage: 0,
    showUser: false, //用户信息显示
    baseInfo: {
      id: NaN,
      username: '未登录',
      sex: '',
      telephone: '',
      avator: '',
      token: '',
      mail: '',
    },
    signupInfo: {
      username: '',
      password: '',
      password2: '',
      telephone: '',
      avator: '',
      email: '',
      sex: '',
      code: '',
    },
    fav: {
      list: [] as any,
      page: 1,
      total: 0,
    },
    subscribe: [] as any,
    comments: [] as any,
    details: {} as Record<number, any>,
    pics: {} as Record<number, any>,
    pages: {
      home: {
        list: [] as number[],
        page: 1,
        total: 0,
      },
      sub: {
        list: [] as number[],
        page: 1,
        total: 0,
        cate: -1,
      },
      search: {
        list: [] as number[],
        page: 1,
        total: 0,
      },
      saying: {
        intro: '',
        page: 1,
        total: 0,
        list: [] as any[],
      },
      podcast: {
        intro: '',
        page: 1,
        total: 0,
        list: [] as any[],
      },
      res: {
        intro: '',
        page: 1,
        total: 0,
        list: [] as any[],
      },
      roomIntro: '',
    },
    resources: {} as Record<number, any>,
    curArticle: '' as any,
    curComment: '' as any,
    notices: [] as any,
    hotDisscussions: [] as any,
    discussionCates: [] as any,
    knowNickName: [] as any,
    enrolledNotices: null as any,
  }),
  getters: {
    articleNum: (state) =>
      Math.min(state.pages.home.list.length, state.pages.home.total),
    subArticleNum: (state) =>
      Math.min(state.pages.sub.list.length, state.pages.sub.total),
    searchArticleNum: (state) =>
      Math.min(state.pages.search.list.length, state.pages.search.total),
    curHtml: (state) => {
      return toHtml(state.curArticle.content);
      // if (state.curArticle.contentRes == undefined) {
      //   return ''
      // } else {
      //   return toHtml(state.curArticle.content)
      // }
    },
    // allpages:(state)=>Math.ceil(state.allcount/state.perpage)*10,
    // searchpages:(state)=>Math.ceil(state.searchcount/state.persearch)*10,
    // avartarimg:(state)=>{
    //   if(state.avatar){
    //     return "https://static.tiaozhan.com"+state.avatar
    //   }else{
    //     return tenzor
    //   }
    // }
    // doubleCount: (state) => state.counter * 2
  },
  actions: {
    increment() {
      // this.counter++
    },
    // getDetailid(article_id: number) {
    //   for (let i = 0; i < this.details.length; i++) {
    //     if(article_id == this.details[i].newsId) return i
    //   }
    //   return -1
    // }
  },
});
